import {Injectable} from '@angular/core';
import {ConfigService} from "./config.service";
import {HttpClient, HttpHeaders} from "@angular/common/http";

@Injectable({
    providedIn: 'root'
})
export class AuthService {

    localstorage_user_key: string = 'angular_user';

    user: any;
    private authUrl = this.configService.api.url + '/auth';

    constructor(private http: HttpClient, private configService: ConfigService) {
    }

    login(user: any): any {
        return this.http.post(this.authUrl + "/login", {user: user});
    }

    verify() : any {
        const user = this.getUserFromStorage();
        return this.http.post(this.authUrl + "/verify", {token: user.token});
    }

    /*async verify2(): Promise<boolean> {
        const user = this.getUserFromStorage();
        const response:any = await this.http.post(this.authUrl + "/verify", {token: this.user.token}).toPromise();
        if (response.result) {
            this.setToken(response.token);
        }
        return response.result;
    }*/

    getToken() {
        const user = this.getUserFromStorage();
        if (user) {
            return user.token;
        }else{
            return "";
        }
    }

    setToken(user, token) {
        this.user = user;
        this.user.token = token;
        //const group = this.user.group;
        //delete this.user.group;
        this.setUserToStorage(this.user);
        //this.user.group = group;
    }

    getHeader() {
        return new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + this.getToken()
        });
    }

    getUserFromStorage() {
        if (!this.user) {
            this.user = JSON.parse(localStorage.getItem(this.localstorage_user_key));
        }
        return this.user;
    }

    setUserToStorage(user: any) {
        this.user = user;
        localStorage.setItem(this.localstorage_user_key, JSON.stringify(user));
    }

    logout() {
        this.user = null;
        localStorage.removeItem(this.localstorage_user_key);
    }

    /*hasPrivilege(name):boolean {
        const user = this.getUserFromStorage();
        if (user && user.groups) {
            const privileges = user.groups.flatMap(group => group.privileges);
            const privilege = privileges.find(p =>{ return p.name === name; });
            return privilege !=  undefined;
        } else if (user && user.group) {
            const privilege = user.group.privileges.find(p =>{ return p.name === name; });
            return privilege !=  undefined;
        } else {
            return false;
        }
    }*/
    hasPrivilege(module): boolean {
        const user = this.getUserFromStorage();
        if (user && Array.isArray(user.groups)) {
            const privilege = user.groups.find(group =>{ return group.modules.find(item => item.code == module) });
            return privilege !=  undefined;
        } else {
            return false;
        }
    }

    canActivate() {
        if (localStorage.getItem(this.localstorage_user_key)) {
            return true;
        } else {
            return false;
        }
    }
}
